作者:有些人笨的不行 | 来源:互联网 | 2023-05-19 00:55
编码插件(Codec)Codec是logstash从1.3.0版开始新引入的概念(Codec来自Coderdecoder两个单词的首字母缩写)。在此之前,logstash只
编码插件(Codec)
- log_format json '{"@timestamp":"$time_iso8601",'
- '"@version":"1",'
- '"host":"$server_addr",'
- '"client":"$remote_addr",'
- '"size":$body_bytes_sent,'
- '"responsetime":$request_time,'
- '"domain":"$host",'
- '"url":"$uri",'
- '"status":"$status"}';
- access_log /logs/nginx/access.log json;
重启nginx使其生效。
设置测试使用的logstash配置文件webnginx.conf:
- input {
- file {
- path => "/logs/nginx/access.log"
- type => "nginx"
- start_position => "beginning"
- add_field => { "key"=>"value"}
- codec => "json"
- }
-
- }
- output {
- stdout{
- codec => rubydebug{ }
- }
- }
logstash加载启动测试:
- logstash -f webnginx.conf
访问测试地址,显示如下(这里我将www.elk.com定向到了nginx所在的虚拟机)
- {
- "type" => "nginx",
- "url" => "/app/panels/timepicker/module.html",
- "tags" => [],
- "path" => "/logs/nginx/access.log",
- "@timestamp" => 2017-02-13T17:02:47.000Z,
- "size" => 2397,
- "domain" => "www.elk.com",
- "@version" => "1",
- "host" => "192.168.1.104",
- "client" => "192.168.2.16",
- "responsetime" => 0.0,
- "key" => "value",
- "status" => "200"
- }
-
补充说明:
日志格式中统一记录为字符串格式(即都带上双引号 “),然后再在 logstash 中用 filter/mutate 插件来变更应该是数值类型的字符字段的值类型。
2,合并多行数据(Multiline)
- input {
- stdin {
- codec => multiline {
- pattern => "^\["
- negate => true
- what => "previous"
- }
- }
- }
- output {
- stdout {
- codec => rubydebug{ }
- }
- }
-
加载:logstash -f multiline.conf
效果如下:
- hello world
- hello logstash
- hello multiline
- [
- {
- "@timestamp" => 2017-02-13T17:29:47.658Z,
- "@version" => "1",
- "host" => "0.0.0.0",
- "message" => "[\nhello world\nhello logstash\nhello multiline",
- "tags" => [
- [0] "multiline"
- ]
- }
-
这个插件的原理很简单,就是把当前行的数据添加到前面一行后面,直到新进的当前行匹配 ^\[ 正则为止。这个正则还可以用 grok 表达式。
参考文章:
http://kibana.logstash.es/content/logstash/plugins/codec/multiline.html